package com.jiai.cloud.sys.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.jiai.cloud.base.exception.ServiceException;
import com.jiai.cloud.base.page.CustomPage;
import com.jiai.cloud.base.tools.RestResultGenerator;
import com.jiai.cloud.entity.TApplication;
import com.jiai.cloud.sys.service.ITApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * <p>
 * 小程序应用 前端控制器
 * </p>
 *
 * @author 佳爱科技
 * @since 2017-05-27
 */
@RestController
@RequestMapping("/api/v1/application")
public class TApplicationController {
    @Autowired
    private ITApplicationService itApplicationService;


    //增加
    @RequestMapping(value = "/addApplication",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    public Object addApplication(@RequestBody TApplication tApplication)throws ServiceException {
        try{
            itApplicationService.insert(tApplication);
            return RestResultGenerator.genSuccessResult("插入成功");
        }catch (Exception e){
            throw new ServiceException("插入失敗", e.getMessage());
        }
    }

    //通过ID删除
    @RequestMapping(value = "/del/{id}", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object deleteId(@PathVariable("id") Integer id) throws ServiceException{
        try{
            itApplicationService.deleteById(id);
            return RestResultGenerator.genSuccessResult();
        }catch (Exception e){
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }

    //查询所得的
    @RequestMapping(value = "/findByAll/{page}",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    public Object findByAll(@PathVariable("page" ) Integer page1) throws ServiceException{
        try{
            Page<TApplication> page = new Page<>(page1, 20);
            itApplicationService.selectPage(page, null);
            return RestResultGenerator.genSuccessResult(new CustomPage(page));
        }catch (Exception e){
            throw new ServiceException("查询失败", e.getMessage());
        }
    }

    //根据ID查找
    @RequestMapping(value = "/findById/{id}", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object findById(@PathVariable("id") Integer id) throws ServiceException {
        try {
            itApplicationService.selectById(id);
            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }

    //保存和更新
    @RequestMapping(value="/edit",method=RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object edit(TApplication tApplication)throws ServiceException{
        try {
            itApplicationService.insertOrUpdate(tApplication);
            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new  ServiceException("编辑失敗",e.getMessage());
        }
    }



    @RequestMapping(value = "goodslist",method=RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public ModelAndView goodslist(){
        ModelAndView view=new ModelAndView("goods/goodslist");
        try {
            view.addObject("goods",itApplicationService.selectList(null));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return view;
    }

}


